package cn.ichiva.common;


import java.io.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 数据转换
 * @Description
 * @auther @author Qp Group
 * @create 2020-04-15 10:59
 */
public class DataHelper {

    public static byte[] toByte(int a) {
        return new byte[] { (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) };
    }

    public static byte[] toByte(short a) {
        return new byte[] { (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF)  };
    }

    public static byte[] toByte(long x) {
        return ByteBuffer.allocate(Long.BYTES).putLong(0, x).array();
    }

    public static byte[] toByte(float x) {
        return toByte(Float.floatToIntBits(x));
    }

    public static byte[] toByte(double x) {
        return toByte(Double.doubleToRawLongBits(x));
    }

    public static byte[] toByte(Serializable x){
        try(ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream objOut = new ObjectOutputStream(out)){
            objOut.writeObject(x);
            objOut.flush();
            return out.toByteArray();
        }catch (Exception e){}
        return null;
    }

    public static short toShort(byte[] b) {
        return (short) (b[1] & 0xFF | (b[0] & 0xFF) << 8);
    }

    public static char toChar(byte[] b) {
        return (char) toInt(b);
    }

    public static int toInt(byte[] b) {
        return   b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24;
    }

    public static long toLong(byte[] b) {
        ByteBuffer buffer = ByteBuffer.allocate(8).put(b, 0, b.length);
        buffer.flip();
        return buffer.getLong();
    }

    public static float toFloat(byte[] b) {
        return Float.intBitsToFloat(toInt(b));
    }

    public static double toDouble(byte[] b) {
        return Double.longBitsToDouble(toLong(b));
    }

    public static String toString(byte[] b) {
        try {
            return new String(b,"utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] toByte(String txt) {
        try {
            return txt.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object toObject(byte[] b){
        try(ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b))){
            return in.readObject();
        }catch (Exception e){}
        return null;
    }

    public static String encode(byte[] b){
        try {
            return new String(b,"ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] decode(String s){
        try {
            return s.getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String md5(String txt){
        try {
            byte[] bytes = txt.getBytes("utf-8");
            return md5String(bytes);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] md5(byte[] bytes) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static final int MD_RADIX = 32;
    public static String md5String(byte[] bytes) {
        return new BigInteger(md5(bytes)).toString(MD_RADIX);
    }
}